<template>
  <div class='container'>
    <el-dialog
      :title="showTitle"
      :visible="isShow"
      @close="closeDialog"
      class="dialog"
    >
      <el-form ref="artform" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标签" />
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            ref="text" v-model="form.articleBody" class="editor" :options="editorOption"
            aria-placeholder="''"
          />
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL" style="margin-top:10px">
          <el-input v-model="form.videoURL" placeholder="请输入视频地址" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  name: 'ArticlesAdd',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    showTitle () {
      return this.form.id ? '编辑文章' : '添加文章'
    }
  },
  data () {
    return {
      content: '',
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '标题内容不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '标题名称只能是1-30个字符', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: ['blur', 'change'] }
        ]
      },
      editorOption: {
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
              [{ list: 'ordered' }, { list: 'bullet' }], // 列表
              ['blockquote', 'code-block'], // 引用，代码块
              ['image'], // 上传图片、上传视频
              ['link']
            ]
          }
        }
      }
    }
  },
  methods: {
    closeDialog () {
      this.$emit('update:isShow', false)
      this.$refs.artform.resetFields()
      this.form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    handleSubmit () {
      this.$refs.artform.validate(async (flag) => {
        if (!flag) return
        if (this.form.id) {
          await update(this.form)
          this.$message.success('编辑文章成功')
        } else {
          await add(this.form)
          this.$message.success('添加文章成功')
        }
        this.closeDialog()
        this.$emit('refresh')
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.editor{
   ::v-deep {

      .el-dialog {
        width: 800px;

        .el-dialog__footer {
            text-align: right;
         }
      }

    .ql-toolbar.ql-snow {
    border: 1px solid #ccc;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
    padding: 0;
    }

    .ql-editor {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    line-height: 1.42;
    height: 200px;
    outline: none;
    overflow-y: auto;
    padding: 12px 15px;
    -o-tab-size: 4;
    tab-size: 4;
    -moz-tab-size: 4;
    text-align: left;
    white-space: pre-wrap;
    word-wrap: break-word;
    }

  }

}

</style>
